<template>
  <div class="container">
    <el-card v-if="pageType === 'list' || 'fitPage' !== 'fitPage'">
      <el-form
        label-position="left"
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
<!--        <el-form-item label="消息内容" prop="messageContent">-->
<!--          <el-input-->
<!--            v-model="queryParams.messageContent"-->
<!--            placeholder="请输入消息内容"-->
<!--            clearable-->
<!--            @keyup.enter.native="handleQuery"-->
<!--          />-->
<!--        </el-form-item>-->
        <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择状态">
            <el-option
              v-for="item in statusList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="发送时间" prop="createTime">-->
<!--          <el-date-picker-->
<!--            v-model="queryParams.createTime"-->
<!--            type="date"-->
<!--            placeholder="选择发送时间"-->
<!--            format="yyyy-MM-dd"-->
<!--            value-format="yyyy-MM-dd"-->
<!--          />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="接收人" prop="recipient">-->
<!--          <el-select v-model="queryParams.recipient" placeholder="请选择接收人">-->
<!--            <el-option-->
<!--              v-for="item in userList"-->
<!--              :key="item.userId"-->
<!--              :label="item.nickName"-->
<!--              :value="item.userId"-->
<!--            >-->
<!--            </el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索
          </el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置
          </el-button>
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain size="mini" @click="handleRead"
            >全部已读</el-button
          >
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>
      <el-table border v-loading="loading" :data="dataList">
        <el-table-column label="序号" type="index" align="center">
          <template #default="{ $index }">
            {{
              $index +
              1 +
              queryParams.pageNum * queryParams.pageSize -
              queryParams.pageSize
            }}
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          align="center"
          label="消息内容"
          prop="messageContent"
          width="400px"
        />
        <el-table-column align="center" label="发送者" prop="sender" />
        <el-table-column align="center" label="接收人" prop="recipientName" />
        <el-table-column align="center" label="发送方式">
          <template #default="{ row }">
            {{ row.sendMode == 0 ? "触发发送" : "定时发送" }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="发送状态">
          <template #default="{ row }">
            {{ row.status == 0 ? "已发送" : "未发送" }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="提醒时间" prop="reminderTime" />
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="handleInfo(scope.row)"
            >查看
            </el-button>
            <el-button size="mini" type="text" @click="handleDel(scope.row.id)"
              >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="paging-container"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryParams.pageNum"
        :page-sizes="[5, 10, 15, 20, 50]"
        :page-size="queryParams.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <el-card v-if="pageType === 'show'">
      <div slot="header" class="table-data-header">
        <div class="header-left">
          <el-button
            class="return-text"
            type="text"
            icon="el-icon-back"
            @click="returnPage"
            >返回
          </el-button>
          <div class="header-title">消息查看</div>
        </div>
      </div>
      <el-descriptions class="margin-top" :column="1" border>
        <el-descriptions-item>
          <template slot="label"> 消息内容 </template>
          {{ formData.messageContent }}
        </el-descriptions-item>
      </el-descriptions>
      <el-descriptions class="margin-top" :column="3" border>
        <el-descriptions-item>
          <template slot="label"> 发送者 </template>
          {{ formData.sender }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label"> 接收人 </template>
          {{ formData.recipientName }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label"> 发送方式 </template>
          {{ formData.sendMode == 0 ? "触发发送" : "定时发送" }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label"> 发送状态 </template>
          {{ formData.status == 0 ? "已发送" : "未发送" }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label"> 提醒时间 </template>
          {{ formData.reminderTime }}
        </el-descriptions-item>
      </el-descriptions>
    </el-card>
  </div>
</template>
<script>
import { getListApi, delApi, getReadListApi } from "../../api/message-info/index";
export default {
  name: "message-remind",
  data() {
    return {
      statusList: [
        { label: "已发送", value: 0 },
        { label: "未发送", value: 1 },
      ],
      form: {},
      pageType: "list",
      loading: false,
      showSearch: true,
      dataList: [],
      total: 0,
      title: "",
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        messageContent: null,
        status: null,
        recipient: null,
        createTime: null,
      },
      subordinateData: {},
      formJson: {},
      planExecutionId: null,
      formData:{},
    };
  },
  mounted() {
    this.getList();
  },

  methods: {
    getList() {
      this.loading = true;
      getListApi(this.queryParams).then((response) => {
        this.dataList = response.rows;
        console.log(response.rows, "list");
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        messageContent: null,
        status: null,
        recipient: null,
        createTime: null,
      };
      this.handleQuery();
    },


    // 一键已读
    handleRead() {
      // getReadListApi
      this.$confirm("您是否要一键读取所有消息?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await getReadListApi();
          if (res.code == 200) {
            this.$message({
              type: "success",
              message: res.msg,
            });
          } else {
            this.$message({
              type: "error",
              message: res.msg,
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleInfo(data){
      this.pageType = 'show'
      this.formData = data
    },
    handleDel(id) {
      const name = this.dataList.filter((item) => item.id == id)[0]
        .messageContent;
      this.$confirm(`您是否要删除消息内容为${name}的通知`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await delApi(id);
          if (res.code == 200) {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getList();
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val;
      this.getList();
    },
    handleSizeChange(val) {
      this.queryParams.pageSize = val;
      this.getList();
    },
    returnPage() {
      this.pageType = "list";
    },
  },
};
</script>
<style lang="scss" scoped>
.container {
  height: calc(100vh - 85px);
  padding: 10px;

  .el-card ::v-deep .el-card__body {
    height: calc(100vh - 160px) !important;
    overflow-y: scroll !important;
  }

  .table-data-header {
    display: flex;
    justify-content: space-between;

    .header-left {
      display: flex;
      align-items: center;

      .return-text {
        color: #000000;
        padding-right: 20px;
      }

      .header-title {
        border-left: 1px solid #dcdfe6;
        padding-left: 20px;
        font-size: 18px;
      }
    }
  }

  .paging-container {
    text-align: right;
    margin-top: 10px;
  }
}
</style>
